from django.core.management.base import BaseCommand
from apps.product.models import *
from apps.user.models import *
from typing import List


class Command(BaseCommand):
    def handle(self, *args, **options):
        ps = Product.objects.all()  # type: List[Product]
        for p in ps:
            today_price_data = p.today_price_data
            week_price_data = p.week_price_data
            month_price_data = p.month_price_data

            if len(today_price_data['points']) > 6:
                today_price_data['points'] = today_price_data['points'][:6]
            if len(week_price_data['points']) > 7:
                week_price_data['points'] = week_price_data['points'][:7]
            if len(month_price_data['points']) > 4:
                month_price_data['points'] = month_price_data['points'][:4]
            p.today_price_data = today_price_data
            p.week_price_data = week_price_data
            p.month_price_data = month_price_data

            p.save()

        us = User.objects.all()  # type: List[User]
        for u in us:
            today_total_value_data = u.today_total_value_data
            current_week_total_value_data = u.current_week_total_value_data
            current_month_total_value_data = u.current_month_total_value_data

            if len(today_total_value_data['points']) > 6:
                today_total_value_data['points'] = today_total_value_data['points'][:6]
            if len(current_week_total_value_data['points']) > 7:
                current_week_total_value_data['points'] = current_week_total_value_data['points'][:7]
            if len(current_month_total_value_data['points']) > 4:
                current_month_total_value_data['points'] = current_month_total_value_data['points'][:4]
            u.today_total_value_data = today_total_value_data
            u.current_week_total_value_data = current_week_total_value_data
            u.current_month_total_value_data = current_month_total_value_data

            u.save()